<?php

class OrderController extends UserController
{
	public function init()
	{
		parent::init();
		$this->userleftsign = 'order';
	}

	public function indexAction()
	{
		$this->title='我的订单';
		$page=I('get.page','int',1);
		$pagesize=10;
		$this->list=M('order')->where(array('user_id'=>$this->userinfo['id']))->page($page)->limit($pagesize)->order('id desc')->getlist();
		$this->pagestr=page::show(M('order')->where(array('user_id'=>$this->userinfo['id']))->count(),$pagesize,$page);
		$this->display();
	}

	public function detailAction()
	{
		$this->title='订单详情';
		$orderid=I('get.orderid','str','');
		$this->info=M('order')->getinfo($orderid);
		$this->display();
	}

    public function gopayAction()
    {
        $orderid=I('post.id','str','');
        $orderinfo=M('order')->getinfo($orderid);
        $param['pay_jb']=min($this->userinfo['extcredits4'],abs(I('post.pay_jb')));
        $param['pay_yb']=min($this->userinfo['extcredits3'],abs(I('post.pay_yb')));
        $param['pay_money']=min($this->userinfo['money'],abs(I('post.money')));
        $param['pay_rmb']=max(0,$orderinfo['totalprice']-$param['pay_jb']-$param['pay_jb']-$param['pay_jb']);
        if ($param['pay_rmb']==0){
            $param['status']=1;
            M('order')->where(array('id'=>$orderinfo['id']))->update($param);
            if ($orderinfo['type']=='app'){
                M('app')->where(array('id'=>$orderinfo['type_id']))->update(array('status'=>1));
            }
            $this->success('支付订单成功',$orderinfo['url_goods']);
        }else{
            //$this->error('我们暂时未开通在线支付，请您联系管理员充值后购买！');
            $param['pay_rmb']=0.01;
            $data = array(
                'service' => 'trade_create_by_buyer',
                'payment_type' => '1',
                '_input_charset' => 'utf-8',
                'partner' => C('pay_alipay_id'),
                'seller_email' => C('pay_alipay_email'),
                'notify_url' => PT_URL.U('user.order.notify'),
                'return_url' => PT_URL.U('user.order.detail',array('orderid'=>$orderinfo['order_id'])),
                'out_trade_no' => $orderinfo['order_id'],
                'subject' => 'PTCMS工作室服务购买',
                'body' => $orderinfo['title'].' '.$orderinfo['addon'],
                'price' => number_format($param['pay_rmb'],2),
                'quantity' => 1,
                "logistics_fee"	=> '0.00',
                "logistics_type"	=> 'EXPRESS',
                "logistics_payment"	=> 'SELLER_PAY',
                'show_url'=>'',
            );
            ksort($data);
            reset($data);
            $arg = '';
            foreach ($data as $key => $value) {
                if ($key!='sign_type' && $key!='sign' && $value) {
                    $arg .= "$key=$value&";
                }
            }

            $data['sign'] = md5(substr($arg, 0, -1) .C('pay_alipay_key'));
            $data['sign_type'] = 'MD5';
            $url='https://mapi.alipay.com/gateway.do?'.http_build_query($data);

            echo '<br /><a href="'.$url.'" target="_blank">支付</a>';
        }
    }
}